import request from '@/utils/request';
import { requestWrapper, randomInt, randomDate } from '@/utils/mock';

// 生成随机日期时间字符串的辅助函数
const generateDateTimeString = () => {
  const startYear = 2023;
  const endYear = new Date().getFullYear();
  const year = randomInt(startYear, endYear);
  const month = String(randomInt(1, 12)).padStart(2, '0');
  const day = String(randomInt(1, 28)).padStart(2, '0');
  const hour = String(randomInt(0, 23)).padStart(2, '0');
  const minute = String(randomInt(0, 59)).padStart(2, '0');
  const second = String(randomInt(0, 59)).padStart(2, '0');
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
};

// 上传文件
export function uploadFile(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/file/upload',
      method: 'post',
      data,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }),
    // 模拟数据
    () => {
      const fileId = Date.now().toString();
      const fileTypes = {
        'avatar': 'image/jpeg',
        'document': 'application/pdf',
        'image': 'image/png',
        'other': 'application/octet-stream'
      };
      const fileType = data.get('type') || 'other';
      const fileName = data.get('file')?.name || `file-${fileId}`;
      
      return {
        fileId,
        fileName,
        originalName: fileName,
        fileType: fileTypes[fileType] || fileTypes.other,
        fileSize: randomInt(10000, 5000000),
        fileUrl: `http://example.com/upload/${fileType}/${fileId}-${fileName}`
      };
    }
  );
}

// 获取文件列表
export function getFileList(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/file/list',
      method: 'get',
      params
    }),
    // 模拟数据
    () => {
      const pageNum = params?.pageNum || 1;
      const pageSize = params?.pageSize || 10;
      const total = 35; // 模拟总数
      
      const fileTypes = ['image/jpeg', 'image/png', 'application/pdf', 'application/msword', 'text/plain', 'application/zip'];
      const modules = ['system', 'user', 'product', 'order', 'content'];
      const userNames = ['admin', 'test', 'user1', 'manager', 'operator'];
      
      const list = Array.from({ length: Math.min(pageSize, total - (pageNum - 1) * pageSize) }, (_, index) => {
        const fileId = `file-${Date.now() + index}`;
        const isImage = Math.random() > 0.5;
        const fileType = fileTypes[randomInt(0, fileTypes.length - 1)];
        const fileName = isImage ? `image-${index + 1}.jpg` : `document-${index + 1}.pdf`;
        const originalName = isImage ? `screenshot-${index + 1}.jpg` : `report-${index + 1}.pdf`;
        
        return {
          fileId,
          fileName,
          originalName,
          fileType,
          fileSize: randomInt(10000, 5000000),
          fileUrl: `http://example.com/upload/${isImage ? 'image' : 'document'}/${fileId}-${fileName}`,
          module: modules[randomInt(0, modules.length - 1)],
          createBy: userNames[randomInt(0, userNames.length - 1)],
          createTime: generateDateTimeString()
        };
      });
      
      return {
        list,
        total,
        pageNum: parseInt(pageNum),
        pageSize: parseInt(pageSize)
      };
    }
  );
}

// 下载文件
export function downloadFile(fileUrl) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/file/download',
      method: 'get',
      params: { fileUrl },
      responseType: 'blob'
    }),
    // 模拟数据 - 对于下载操作，模拟数据意义不大，但我们提供一个成功响应
    () => new Blob(['Simulated file content'], { type: 'application/octet-stream' })
  );
}

// 删除文件
export function deleteFile(fileIds) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/file/delete',
      method: 'delete',
      data: { fileIds } // 修改为fileIds参数名
    }),
    // 模拟数据
    {
      success: true
    }
  );
}

// 获取文件预览链接
export function getPreviewUrl(fileId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/file/preview/${fileId}`,
      method: 'get'
    }),
    // 模拟数据
    {
      previewUrl: `http://example.com/preview/${fileId}`,
      expireTime: 3600
    }
  );
} 